package service

import (
	"server/global"
	"server/plugin/mall/model"
	"server/plugin/mall/model/request"
)

var A价格表 = new(jiagebiao)

type jiagebiao struct{}

// CreateA价格表 创建价格表记录
// Author [yourname](https://github.com/yourname)
func (s *jiagebiao) CreateA价格表(jiagebiao *model.A价格表) (err error) {
	err = global.GVA_DB.Create(jiagebiao).Error
	return err
}

// DeleteA价格表 删除价格表记录
// Author [yourname](https://github.com/yourname)
func (s *jiagebiao) DeleteA价格表(ID string) (err error) {
	err = global.GVA_DB.Delete(&model.A价格表{}, "id = ?", ID).Error
	return err
}

// DeleteA价格表ByIds 批量删除价格表记录
// Author [yourname](https://github.com/yourname)
func (s *jiagebiao) DeleteA价格表ByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]model.A价格表{}, "id in ?", IDs).Error
	return err
}

// UpdateA价格表 更新价格表记录
// Author [yourname](https://github.com/yourname)
func (s *jiagebiao) UpdateA价格表(jiagebiao model.A价格表) (err error) {
	err = global.GVA_DB.Model(&model.A价格表{}).Where("id = ?", jiagebiao.ID).Updates(&jiagebiao).Error
	return err
}

// GetA价格表 根据ID获取价格表记录
// Author [yourname](https://github.com/yourname)
func (s *jiagebiao) GetA价格表(ID string) (jiagebiao model.A价格表, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&jiagebiao).Error
	return
}

// GetA价格表InfoList 分页获取价格表记录
// Author [yourname](https://github.com/yourname)
func (s *jiagebiao) GetA价格表InfoList(info request.A价格表Search) (list []model.A价格表, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.A价格表{})
	var jiagebiaos []model.A价格表
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}
	err = db.Find(&jiagebiaos).Error
	return jiagebiaos, total, err
}
func (s *jiagebiao) GetA价格表DataSource() (res map[string][]map[string]any, err error) {
	res = make(map[string][]map[string]any)

	人数 := make([]map[string]any, 0)

	global.GVA_DB.Table("规格设置").Where("deleted_at IS NULL").Select("适合人数 as label,id as value").Scan(&人数)
	res["人数"] = 人数
	规格 := make([]map[string]any, 0)

	global.GVA_DB.Table("规格设置").Where("deleted_at IS NULL").Select("规格 as label,id as value").Scan(&规格)
	res["规格"] = 规格
	return
}

func (s *jiagebiao) GetA价格表Public() {

}
